﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-15 17:34:59
 * 功能：界面【PreviewPanel】的View
        1、负责收集所有控件
**************************************************************************************/

using Game.Framework;
using Game.UI.HallPanel;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.PreviewPanel
{
    public class PreviewPanelView : PopupPanel
    {
        public const string PANEL_KEY = "Game_UI_PreviewPanel_PreviewPanel_Key";
        private GalleryItem m_GalleryItem;
        private Item m_Item, m_Item1, m_Cur, m_Next;
        private int m_NeedCoins, m_NeedAdCount;
        private EGalleryType m_Type;

        protected override void RegistAny()
        {
            PanelManager.Instance.RegistPanel(PANEL_KEY, this);
        }

        protected override void UnregistAny()
        {
            PanelManager.Instance.UnregistPanel(PANEL_KEY);
        }

        protected override void InitUI()
        {
            m_Item = transform.Find("Mask/Item").GetComponent<Item>();
            m_Item1 = transform.Find("Mask/Item1").GetComponent<Item>();
        }

        public override void Init(object data)
        {
            EventCenter.Instance.AddEventListen<EAdType, ETriggerAdType>(GameEventName.ON_SHOW_SUCCESS_AD, CloseSuccessAd);
            m_GalleryItem = data as GalleryItem;
            m_Type = m_GalleryItem.Type;
            m_Item.Init(m_GalleryItem.Icon);
            m_Cur = m_Item;
            m_Next = m_Item1;
            UpdateBtnStatus();
            UIUtility.TopDisplayLobbyPanelInfo(true, true);
        }

        private void UpdateBtnStatus()
        {
            GetControl<Toggle>(PreviewPanelNotifications.CollectBtn_CTRL).SetIsOnWithoutNotify(m_GalleryItem.Data.collectStatus == ECollectStatus.Collected);
            GetControl<Button>(PreviewPanelNotifications.PreviousBtn_CTRL).gameObject.SetActive(m_GalleryItem.Previous != null);
            GetControl<Button>(PreviewPanelNotifications.NextBtn_CTRL).gameObject.SetActive(m_GalleryItem.Next != null);
            var lvInfo = m_GalleryItem.Data;
            m_NeedAdCount = lvInfo.level <= BaseDataMgr.Instance.Cfg.level.level_data.Length ? BaseDataMgr.Instance.Cfg.level.level_data[lvInfo.level - 1].download_ads : BaseDataMgr.Instance.Cfg.level.other_level.download_ads;
            m_NeedCoins = lvInfo.level <= BaseDataMgr.Instance.Cfg.level.level_data.Length ? BaseDataMgr.Instance.Cfg.level.level_data[lvInfo.level - 1].download_cost : BaseDataMgr.Instance.Cfg.level.other_level.download_cost;
            bool isCanFreeDownload = lvInfo.downloadAdCount >= m_NeedAdCount;

            GetControl<Button>(PreviewPanelNotifications.CoinBtn_CTRL).gameObject.SetActive(!isCanFreeDownload);
            GetControl<Button>(PreviewPanelNotifications.ClaimBtn_CTRL).gameObject.SetActive(!isCanFreeDownload);
            GetControl<Button>(PreviewPanelNotifications.DownloadBtn_CTRL).gameObject.SetActive(isCanFreeDownload);
            if (!isCanFreeDownload)
            {
                GetControl<Text>(PreviewPanelNotifications.CoinNum_CTRL).text = $"{BaseDataMgr.Instance.GetLanguageValue("Download")}  {m_NeedCoins}";
                GetControl<Text>(PreviewPanelNotifications.ClaimNum_CTRL).text = $"{BaseDataMgr.Instance.GetLanguageValue("Download")}  ( {lvInfo.downloadAdCount}/{m_NeedAdCount} )";
            }
        }

        private void OnDisable()
        {
            EventCenter.Instance.RemoveEventListen<EAdType, ETriggerAdType>(GameEventName.ON_SHOW_SUCCESS_AD, CloseSuccessAd);
            m_Item.SetPos(Vector2.zero, false);
            m_Item1.SetPos(Vector2.right * -810, false);
            UIUtility.TopDisplayLobbyPanelInfo(false, true);
        }

        protected override void ClickBtnHandle(string name)
        {
            switch (name)
            {
                case PreviewPanelNotifications.CloseBtn_CTRL:
                    UIUtility.HidePanel(PANEL_KEY);
                    break;

                case PreviewPanelNotifications.PreviousBtn_CTRL:
                    m_Next.Init(m_GalleryItem.Previous.Icon);
                    m_Next.SetPos(Vector2.right * -810, false);
                    m_Cur.SetPos(Vector2.right * 810, true);
                    m_Next.SetPos(Vector2.zero, true);
                    m_Cur = m_Cur == m_Item ? m_Item1 : m_Item;
                    m_Next = m_Next == m_Item1 ? m_Item : m_Item1;
                    m_GalleryItem = m_GalleryItem.Previous;
                    UpdateBtnStatus();
                    break;

                case PreviewPanelNotifications.NextBtn_CTRL:
                    m_Next.Init(m_GalleryItem.Next.Icon);
                    m_Next.SetPos(Vector2.right * 810, false);
                    m_Cur.SetPos(Vector2.right * -810, true);
                    m_Next.SetPos(Vector2.zero, true);
                    m_Cur = m_Cur == m_Item ? m_Item1 : m_Item;
                    m_Next = m_Next == m_Item1 ? m_Item : m_Item1;
                    m_GalleryItem = m_GalleryItem.Next;
                    UpdateBtnStatus();
                    break;

                case PreviewPanelNotifications.SetBtn_CTRL:
                    if (m_GalleryItem.Data.level > 1 && m_GalleryItem.Data.finalSelectIdx != -1)
                        GameDataMgr.Instance.CurHallBgInfo = m_GalleryItem.Data.assetsInfo[m_GalleryItem.Data.finalSelectIdx];
                    else
                        GameDataMgr.Instance.CurHallBgInfo = null;
                    UIUtility.ShowToast(BaseDataMgr.Instance.GetLanguageValue("Successfully set"));
                    break;

                case PreviewPanelNotifications.CoinBtn_CTRL:
                    if (GameDataMgr.Instance.TotalCoinsNum < m_NeedCoins)
                    {
                        UIUtility.ShowToast(BaseDataMgr.Instance.GetLanguageValue("The remaining gold coins are insufficient!!"));
                        break;
                    }
                    m_GalleryItem.Data.downloadAdCount = m_NeedAdCount;
                    DownloadHandle();
                    GameDataMgr.Instance.UpdateCoins(-m_NeedCoins);
                    break;

                case PreviewPanelNotifications.ClaimBtn_CTRL:
                    NativeInteraction.Instance.ShowRewardAd(ETriggerAdType.DownloadImg);
                    break;

                case PreviewPanelNotifications.DownloadBtn_CTRL:
                    DownloadHandle();
                    break;
            }
        }

        private void DownloadHandle()
        {
            string path = m_GalleryItem.Data.level == 1 || m_GalleryItem.Data.finalSelectIdx == -1 ? AssetCacheServer.Instance.DefaultBgPath : AssetCacheServer.Instance.GetAssetPath(m_GalleryItem.Data.assetsInfo[m_GalleryItem.Data.finalSelectIdx].type, m_GalleryItem.Data.assetsInfo[m_GalleryItem.Data.finalSelectIdx].id);
            NativeInteraction.Instance.SaveImageToAlbum(path);
            UpdateBtnStatus();
        }

        protected override void ClickToggleHandle(string name, bool isOn)
        {
            // 收藏，取消收藏
            m_GalleryItem.UpdateCollectBtnStatus(isOn);
            if (m_Type == EGalleryType.Liked && !isOn)
                UIUtility.HidePanel(PANEL_KEY);
        }

        private void CloseSuccessAd(EAdType adType, ETriggerAdType triggerAdType)
        {
            if (adType != EAdType.RewardedVideo || triggerAdType != ETriggerAdType.DownloadImg) return;
            m_GalleryItem.Data.downloadAdCount++;
            UpdateBtnStatus();
            if (m_GalleryItem.Data.downloadAdCount >= m_NeedAdCount)
                DownloadHandle();
        }
    }
}